

using System;
using System.Text;
using QQZiFramework.Membership;

namespace QQZiFramework
{
    
    /// <summary>
    /// 业务访问基本类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class QQZIBaseBusiness : IBusiness
    {       
        public static string DomainServer;

        //BaseUser m_loignUser;

        DBContext m_context;

        /// <summary>
        /// Content目录，用来放下载文件等
        /// </summary>
        public static string Content_Dic_Path;

        /// <summary>
        /// 设置用户信息，由控制层BaseControll调用
        /// </summary>
        /// <param name="user"></param>
        public virtual void SetVisiter(DBContext context)
        {
            if (context != null) m_context = context;
        }

        ///// <summary>
        ///// 登录用户信息
        ///// </summary>
        //public BaseUser LoginUser
        //{
        //    get
        //    {
        //        if (m_loignUser == null) throw new ClientException("LoginUser为空，如果已经登录，请使用CreateBusiness来访问业务类");
        //        return m_loignUser;
        //    }
        //}

        /// <summary>
        /// 重新加载用户
        /// </summary>
        /// <returns></returns>
        public virtual BaseUser QueryUser()
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 更新Redis中的用户信息
        /// </summary>
        public virtual void UpdateItemInCache(IDBEntity item) 
        {
            Context.SetRedisItem(item.GetType().Name + "_" + item.GetPrimaryValue(), item.ToJson());
        }
        /// <summary>
        /// 删除Redis中的项
        /// </summary>
        public virtual void DeleteItemInCache<T>(int primryVal) where T : IDBEntity
        {
            if (DBContext.UserRedis) Context.GetRedisDatabase().DeleteRedisItem(typeof(T).Name + "_" + primryVal);
        }

        public virtual void DeleteItemInCache(Type type,int primaryVal) 
        {
            if (DBContext.UserRedis) Context.GetRedisDatabase().DeleteRedisItem(type.Name + "_" + primaryVal);
        }


        /// <summary>
        /// 获取Redis中的用户信息
        /// <param name="primaryVal">实体主键值</param>
        /// </summary>
        public virtual T GetItemFromCache<T>(int primaryVal, bool throwNull = true)
        {
            string key = typeof(T).Name + "_" + primaryVal;
            return Context.GetRedisItem<T>(key, throwNull);
        }

        /// <summary>
        /// 获取Redis中的用户信息
        /// <param name="key">redis中的键</param>
        /// </summary>
        public virtual object GetItemFromCache(Type type, int primaryVal, bool throwNull = true)
        {
            string key = type.Name + "_" + primaryVal;
            return Context.GetRedisDatabase().GetItemFromCache(type, key, throwNull);
        }

        /// <summary>
        /// 是否使用新版本的Context方式调用
        /// </summary>        
        public bool InNewContext()
        {
            return m_context != null;
        }
        public virtual DBContext Context
        {
            get
            {
                if (m_context == null) throw new ClientException("此处不能使用Context,请在DbContext的using中使用GetBusiness来访问！");
                return m_context;
            }
        }
    }

}